package com.bosssoft.hr.train.jsp.example.service.impl;

import com.bosssoft.hr.train.jsp.example.dao.impl.UserDaoImpl;
import com.bosssoft.hr.train.jsp.example.dao.UserDao;
import com.bosssoft.hr.train.jsp.example.exception.BusinessException;
import com.bosssoft.hr.train.jsp.example.pojo.User;
import com.bosssoft.hr.train.jsp.example.service.UserService;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * @description User Service 提供用户业务
 * @author ChenTong
 * @create 2020-05-30 10:24
 **/
@Slf4j
public class UserServiceImpl implements UserService {
    //实例化数据库操作接口
    private UserDao userDao = new UserDaoImpl();

    /**
     * 保存用户到数据库
     * @param user
     * @return Boolean
     */
    @Override
    public boolean save(User user) {
        try {
            // 判断非空 -> 执行数据库操作 -> 判断结果
            return user!=null&&user.getId()!=null&&user.getPassword()!=null
                    &&user.getCode()!=null&&user.getName()!=null
                    &&-1 != userDao.insert(user);

        }catch (Exception ex){
            log.error(ex.getMessage(),ex);
            throw new BusinessException("10001",ex.getMessage(),ex);
        }

    }

    /**
     * 通过id删除用户
     * @param id : 用户id
     * @return Boolean
     */
    @Override
    public boolean remove(Long id) {
        try {
            //判断非空
            if (id!=null){
                int result = userDao.deleteById(id);
                log.info("remove result"+result);
                if (result!=0){
                    return true;
                }
            }

            return false;

        }catch (Exception e){
            throw new BusinessException("10001",e.getMessage(),e);
        }
    }

    /**
     * 更新用户
     * @param user : 需要更新的用户
     * @return Boolean
     */
    @Override
    public boolean update(User user) {
        try {
            // 判断非空 -> 执行数据库操作 -> 判断结果
            return user!=null&&user.getId()!=null&&user.getPassword()!=null
                    &&user.getCode()!=null&&user.getName()!=null
                    &&-1 != userDao.update(user);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            throw new BusinessException("100",e.getMessage(),e);
        }
    }



    /**
     * 查询所有用户
     * @return List<User>
     */
    @Override
    public List<User> listAllUser(){
        try {
            return userDao.listAllUser();
        }catch (Exception e){
            throw new BusinessException("100",e.getMessage(),e);
        }
    }

    /**
     * 验证用户登录
     * @param user : 需要验证的用户
     * @return boolean
     */
    @Override
    public boolean authentication(User user) {
        try {
            //判单数据参数是否非空
            if (user.getName()==null||user.getPassword()==null) return false;
            //获取数据
            User userDB = userDao.queryUserByName(user.getName());
            //判断登录
            return null!=userDB && user.getPassword().equals(userDB.getPassword());
        } catch (Exception e){
            log.error(e.getMessage(),e);
            throw new BusinessException("321",e.getMessage(),e);
        }
    }
}
